<?php

namespace Content;

use Category\CategoryDAOModel;

/**
 * 内容业务相关
 */
class ContentModel extends \Business\BusinessBase
{
    /**
     * 内容列表功能
     *
     * @param array $params
     * @return array
     */
    public function contentList($params)
    {
        $page = empty($params['page']) ? 1 : $params['page'];
        $count = empty($params['count']) ? 10 : $params['count'];
        $where = empty($params['where']) ? null : $params['where'];
        $order = empty($params['order']) ? null : $params['order'];
        $baseUrl = empty($params['baseUrl']) ? null : $params['baseUrl'];

        $ContentDAOModel = ContentDAOModel::getInstance();
        $totalArr = $ContentDAOModel->fetchAll(array(new \Zend\Db\Sql\Expression('count(0) as total')), $where, null, 1);
        $totalNum = $totalArr[0]['total']; //总数

        $paginator = new \Our\Paginator($page, $totalNum, $count, array('baseUrl' => $baseUrl, 'ext' => '/'));
        $result = $ContentDAOModel->fetchAll(null, $where, array('content_id desc'), $count, ($page - 1) * $count);

        $ContentDAOModel = ContentDAOModel::getInstance();
        $CategoryDAOModel = CategoryDAOModel::getInstance();
        foreach ($result as $key => $value) {
            $result[$key]['status_name'] = $ContentDAOModel->statusConfig[$value['status']]['name'];
            $result[$key]['category'] = $CategoryDAOModel->find($value['category_id']);
        }

        return [$result, $paginator];
    }

    /**
     * 增加内容
     *
     * @param array $params
     *  content_title
     *  content
     *  content_image
     *  status
     * @return false|int
     */
    public function add($params)
    {
        $ContentDAOModel = ContentDAOModel::getInstance();
        $params['add_time'] = time();
        $params['content'] = $this->_formatContent($params['content']);
        $params['update_time'] = $params['add_time'];
        $result = $ContentDAOModel->insert($params);
        if (!$result) {
            return false;
        }
        return $result;
    }

    /**
     * 编辑内容
     *
     * @param array $params
     *  content_title
     *  content
     *  content_image
     *  status
     * @return int
     */
    public function edit($params)
    {
        $ContentDAOModel = ContentDAOModel::getInstance();
        if ($params['add_time']) {
            $params['add_time'] = strtotime($params['add_time']);
        }
        $params['update_time'] = time();
        $params['content'] = $this->_formatContent($params['content']);
//        var_dump($params);exit();
        $result = $ContentDAOModel->update($params, array('content_id' => $params['content_id']));
        return $result;
    }

    /**
     * 删除功能
     *
     * @param $params
     * @return bool
     */
    public function delete($params)
    {
        $ContentDAOModel = ContentDAOModel::getInstance();

        $ContentDAOModel->beginTransaction();
        $result = $ContentDAOModel->update(array('status' => 3), array('content_id' => $params['content_id']));
        if (!$result) {
            $ContentDAOModel->rollback();
            return false;
        }
        $ContentDAOModel->commit();
        return true;
    }

    /**
     * 处理内容
     *
     * @param string $content
     * @return string
     */
    protected function _formatContent($content)
    {
        $content = str_replace(array('&nbsp;', '　'), '', $content);
        $content = str_replace(array('<br />', '<br>'), '<br/>', $content);

        $array = explode('<br/>', $content);
        $array = \Our\Common::arrayTrim($array);

        $string = implode('<br />', $array);

        return $string;
    }

    /**
     * 类实例
     *
     * @var \Content\ContentModel
     */
    private static $_instance = null;

    /**
     * 获取类实例
     *
     * @return \Content\ContentModel
     */
    public static function getInstance()
    {
        if (!(self::$_instance instanceof self)) {
            self::$_instance = new self();
        }

        return self::$_instance;
    }
}
